<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.38" />
    <meta name="theme" content="VuePress Theme Hope" />
    <meta property="og:url" content="https://github.com.io/article/tampermonkey/tm1.html"><meta property="og:site_name" content="Hangover"><meta property="og:title" content="油猴 API"><meta property="og:type" content="article"><meta property="og:locale" content="zh-CN"><meta property="article:tag" content="Tampermonkey"><meta property="article:tag" content="油猴"><meta property="article:tag" content="脚本猫"><meta property="article:published_time" content="2022-04-12T00:00:00.000Z"><link rel="stylesheet" href="//at.alicdn.com/t/font_2410206_mfj6e1vbwo.css"><link rel="stylesheet" href="//at.alicdn.com/t/font_3325523_eud95fd11si.css"><link rel="stylesheet" href="//at.alicdn.com/t/font_3354094_xuy6jxxju39.css"><link rel="icon" href="/favicon.ico"><title>油猴 API | Hangover</title><meta name="description" content="Hangover的个人博客">
    <style>
      :root {
        --bg-color: #fff;
      }

      html[data-theme="dark"] {
        --bg-color: #1d2025;
      }

      html,
      body {
        background-color: var(--bg-color);
      }
    </style>
    <script>
      const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
      const systemDarkMode =
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches;

      if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
        document.querySelector("html").setAttribute("data-theme", "dark");
      }
    </script>
    <link rel="stylesheet" href="/assets/style.76e0a9d2.css">
    <link rel="modulepreload" href="/assets/app.0d0ab3c5.js"><link rel="modulepreload" href="/assets/tm1.html.a0ed7eea.js"><link rel="modulepreload" href="/assets/tm1.html.d29b2fc6.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper.21dcd24c.js">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="skip-link sr-only">Skip to content</a><!--]--><div class="theme-container has-toc sidebar-open"><!--[--><header class="navbar"><button class="toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><a href="/" class="home-link"><img class="logo" src="/logo.png" alt="Hangover"><!----><span class="site-name hide-in-pad">Hangover</span><!--[--><!----><!--]--></a><nav class="nav-links" style=""><div class="nav-item hide-in-mobile"><a href="/" class="nav-link" aria-label="首页"><i class="icon iconfont icon-home"></i>首页<!----></a></div><div class="nav-item hide-in-mobile"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="文章"><span class="title"><i class="icon iconfont icon-flower"></i>文章</span><span class="arrow"></span><ul class="nav-dropdown"><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>Tampermonkey</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a aria-current="page" href="/article/tampermonkey/tm1.html" class="router-link-active router-link-exact-active nav-link active" aria-label="油猴 API"><i class="icon iconfont icon-a-apichengxushezhichiluncanshu"></i>油猴 API<!----></a></li><li class="dropdown-subitem"><a href="/article/tampermonkey/tm2.html" class="nav-link" aria-label="免费翻译API"><i class="icon iconfont icon-fanyi"></i>免费翻译API<!----></a></li><li class="dropdown-subitem"><a href="/article/tampermonkey/tm3.html" class="nav-link" aria-label="百度翻译API"><i class="icon iconfont icon-Group"></i>百度翻译API<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>JavaScript</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/article/javascript/js1.html" class="nav-link" aria-label="类型转换机制"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>类型转换机制<!----></a></li><li class="dropdown-subitem"><a href="/article/javascript/js2.html" class="nav-link" aria-label="数组常用方法"><i class="icon iconfont icon-random1"></i>数组常用方法<!----></a></li><li class="dropdown-subitem"><a href="/article/javascript/js3.html" class="nav-link" aria-label="字符串常用方法"><i class="icon iconfont icon-string"></i>字符串常用方法<!----></a></li><li class="dropdown-subitem"><a href="/article/javascript/js4.html" class="nav-link" aria-label="防抖与节流"><i class="icon iconfont icon-fangdou"></i>防抖与节流<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>CSS</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/article/css/css1.html" class="nav-link" aria-label="响应式设计"><i class="icon iconfont icon-xiangyingshi"></i>响应式设计<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css2.html" class="nav-link" aria-label="flex弹性布局"><i class="icon iconfont icon-flex"></i>flex弹性布局<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css3.html" class="nav-link" aria-label="grid网格布局"><i class="icon iconfont icon-grid"></i>grid网格布局<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css4.html" class="nav-link" aria-label="视差滚动效果"><i class="icon iconfont icon-shichagundong"></i>视差滚动效果<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css5.html" class="nav-link" aria-label="文本溢出省略"><i class="icon iconfont icon-wenbenyichuzhanshi"></i>文本溢出省略<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css6.html" class="nav-link" aria-label="css画三角形"><i class="icon iconfont icon-sanjiaoxing"></i>css画三角形<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css7.html" class="nav-link" aria-label="css实现动画"><i class="icon iconfont icon-style"></i>css实现动画<!----></a></li><li class="dropdown-subitem"><a href="/article/css/css8.html" class="nav-link" aria-label="隐藏页面元素"><i class="icon iconfont icon-yincangbukejian"></i>隐藏页面元素<!----></a></li></ul></li></ul></button></div></div><div class="nav-item hide-in-mobile"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="代码片段"><span class="title"><i class="icon iconfont icon-template"></i>代码片段</span><span class="arrow"></span><ul class="nav-dropdown"><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>数组方法</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/array/array1.html" class="nav-link" aria-label="数组合并"><i class="icon iconfont icon-shujujihebing-xuanzhong"></i>数组合并<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array2.html" class="nav-link" aria-label="最大/最小值"><i class="icon iconfont icon-zuidazhizuixiaozhi"></i>最大/最小值<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array3.html" class="nav-link" aria-label="Set 对象作用"><i class="icon iconfont icon-quweiyizhi"></i>Set 对象作用<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array4.html" class="nav-link" aria-label="过滤虚假值"><i class="icon iconfont icon-guolvtiaojian"></i>过滤虚假值<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array5.html" class="nav-link" aria-label="数组洗牌"><i class="icon iconfont icon-suijishushengcheng-xuanzhong"></i>数组洗牌<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array6.html" class="nav-link" aria-label="删除重复元素"><i class="icon iconfont icon-shujuguolv"></i>删除重复元素<!----></a></li><li class="dropdown-subitem"><a href="/template/array/array7.html" class="nav-link" aria-label="数组比较"><i class="icon iconfont icon-icon-test"></i>数组比较<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>字符串操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/string/string1.html" class="nav-link" aria-label="字符串和数字互相转换"><i class="icon iconfont icon-geshizhuanhuan-xuanzhong"></i>字符串和数字互相转换<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string2.html" class="nav-link" aria-label="字符串首字母大写"><i class="icon iconfont icon-daxie"></i>字符串首字母大写<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string3.html" class="nav-link" aria-label="翻转字符串"><i class="icon iconfont icon-fanzhuan"></i>翻转字符串<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string4.html" class="nav-link" aria-label="随机字符串"><i class="icon iconfont icon-random"></i>随机字符串<!----></a></li><li class="dropdown-subitem"><a href="/template/string/string5.html" class="nav-link" aria-label="截断字符串"><i class="icon iconfont icon-duankai"></i>截断字符串<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>数字操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/number/number1.html" class="nav-link" aria-label="判断奇数、偶数"><i class="icon iconfont icon-panduan"></i>判断奇数、偶数<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number2.html" class="nav-link" aria-label="计算平均值"><i class="icon iconfont icon-col-avg"></i>计算平均值<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number3.html" class="nav-link" aria-label="随机整数"><i class="icon iconfont icon-suijishushengcheng-xuanzhong"></i>随机整数<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number4.html" class="nav-link" aria-label="四舍五入"><i class="icon iconfont icon-sishewuru"></i>四舍五入<!----></a></li><li class="dropdown-subitem"><a href="/template/number/number5.html" class="nav-link" aria-label="去除数字之外的所有字符"><i class="icon iconfont icon-guolv"></i>去除数字之外的所有字符<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>时间操作</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/time/time1.html" class="nav-link" aria-label="当前时间与倒计时"><i class="icon iconfont icon-daojishi"></i>当前时间与倒计时<!----></a></li><li class="dropdown-subitem"><a href="/template/time/time2.html" class="nav-link" aria-label="提取时间"><i class="icon iconfont icon-shijian"></i>提取时间<!----></a></li><li class="dropdown-subitem"><a href="/template/time/time3.html" class="nav-link" aria-label="时间格式化"><i class="icon iconfont icon-shijianzhuanhuangeshi"></i>时间格式化<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>Promise</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/promise/promise1.html" class="nav-link" aria-label="红绿灯交替"><i class="icon iconfont icon-honglvdeng"></i>红绿灯交替<!----></a></li><li class="dropdown-subitem"><a href="/template/promise/promise2.html" class="nav-link" aria-label="异步加载图片"><i class="icon iconfont icon-loading"></i>异步加载图片<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>JavaScript</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/javascript/js1.html" class="nav-link" aria-label="获取变量类型"><i class="icon iconfont icon-leixing"></i>获取变量类型<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js2.html" class="nav-link" aria-label="华氏度和摄氏度"><i class="icon iconfont icon-wendu"></i>华氏度和摄氏度<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js3.html" class="nav-link" aria-label="动态引入js"><i class="icon iconfont icon-java-script"></i>动态引入js<!----></a></li><li class="dropdown-subitem"><a href="/template/javascript/js4.html" class="nav-link" aria-label="判断类型"><i class="icon iconfont icon-he_76xuexileixingguanli"></i>判断类型<!----></a></li></ul></li><li class="dropdown-item"><h4 class="dropdown-subtitle"><span>CSS</span></h4><ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/template/css/css1.html" class="nav-link" aria-label="loading动画"><i class="icon iconfont icon-loading"></i>loading动画<!----></a></li></ul></li></ul></button></div></div><div class="nav-item hide-in-mobile"><a href="/home.html" class="nav-link" aria-label="项目收藏"><i class="icon iconfont icon-module"></i>项目收藏<!----></a></div><div class="nav-item hide-in-mobile"><a href="/bbs.html" class="nav-link" aria-label="留言板"><i class="icon iconfont icon-mark"></i>留言板<!----></a></div></nav><div class="nav-actions-wrapper"><!--[--><!----><!--]--><div class="nav-item"><!----></div><div class="nav-item"><a class="repo-link" href="https://github.com/zuifanzhangsan" target="_blank" rel="noopener noreferrer"><svg xmlns="http://www.w3.org/2000/svg" class="icon github-icon" viewbox="0 0 1024 1024" aria-labelledby="github" style="width:1.25rem;height:1.25rem;vertical-align:middle;"><title id="github" lang="en">github icon</title><g fill="currentColor"><path d="M511.957 21.333C241.024 21.333 21.333 240.981 21.333 512c0 216.832 140.544 400.725 335.574 465.664 24.49 4.395 32.256-10.07 32.256-23.083 0-11.69.256-44.245 0-85.205-136.448 29.61-164.736-64.64-164.736-64.64-22.315-56.704-54.4-71.765-54.4-71.765-44.587-30.464 3.285-29.824 3.285-29.824 49.195 3.413 75.179 50.517 75.179 50.517 43.776 75.008 114.816 53.333 142.762 40.79 4.523-31.66 17.152-53.377 31.19-65.537-108.971-12.458-223.488-54.485-223.488-242.602 0-53.547 19.114-97.323 50.517-131.67-5.035-12.33-21.93-62.293 4.779-129.834 0 0 41.258-13.184 134.912 50.346a469.803 469.803 0 0 1 122.88-16.554c41.642.213 83.626 5.632 122.88 16.554 93.653-63.488 134.784-50.346 134.784-50.346 26.752 67.541 9.898 117.504 4.864 129.834 31.402 34.347 50.474 78.123 50.474 131.67 0 188.586-114.73 230.016-224.042 242.09 17.578 15.232 33.578 44.672 33.578 90.454v135.85c0 13.142 7.936 27.606 32.854 22.87C862.25 912.597 1002.667 728.747 1002.667 512c0-271.019-219.648-490.667-490.71-490.667z"></path></g></svg></a></div><div class="nav-item hide-in-mobile"><button class="outlook-button" tabindex="-1" ariahidden="true"><svg xmlns="http://www.w3.org/2000/svg" class="icon outlook-icon" viewbox="0 0 1024 1024" aria-labelledby="outlook"><title id="outlook" lang="en">outlook icon</title><g fill="currentColor"><path d="M224 800c0 9.6 3.2 44.8 6.4 54.4 6.4 48-48 76.8-48 76.8s80 41.6 147.2 0 134.4-134.4 38.4-195.2c-22.4-12.8-41.6-19.2-57.6-19.2C259.2 716.8 227.2 761.6 224 800zM560 675.2l-32 51.2c-51.2 51.2-83.2 32-83.2 32 25.6 67.2 0 112-12.8 128 25.6 6.4 51.2 9.6 80 9.6 54.4 0 102.4-9.6 150.4-32l0 0c3.2 0 3.2-3.2 3.2-3.2 22.4-16 12.8-35.2 6.4-44.8-9.6-12.8-12.8-25.6-12.8-41.6 0-54.4 60.8-99.2 137.6-99.2 6.4 0 12.8 0 22.4 0 12.8 0 38.4 9.6 48-25.6 0-3.2 0-3.2 3.2-6.4 0-3.2 3.2-6.4 3.2-6.4 6.4-16 6.4-16 6.4-19.2 9.6-35.2 16-73.6 16-115.2 0-105.6-41.6-198.4-108.8-268.8C704 396.8 560 675.2 560 675.2zM224 419.2c0-28.8 22.4-51.2 51.2-51.2 28.8 0 51.2 22.4 51.2 51.2 0 28.8-22.4 51.2-51.2 51.2C246.4 470.4 224 448 224 419.2zM320 284.8c0-22.4 19.2-41.6 41.6-41.6 22.4 0 41.6 19.2 41.6 41.6 0 22.4-19.2 41.6-41.6 41.6C339.2 326.4 320 307.2 320 284.8zM457.6 208c0-12.8 12.8-25.6 25.6-25.6 12.8 0 25.6 12.8 25.6 25.6 0 12.8-12.8 25.6-25.6 25.6C470.4 233.6 457.6 220.8 457.6 208zM128 505.6C128 592 153.6 672 201.6 736c28.8-60.8 112-60.8 124.8-60.8-16-51.2 16-99.2 16-99.2l316.8-422.4c-48-19.2-99.2-32-150.4-32C297.6 118.4 128 291.2 128 505.6zM764.8 86.4c-22.4 19.2-390.4 518.4-390.4 518.4-22.4 28.8-12.8 76.8 22.4 99.2l9.6 6.4c35.2 22.4 80 12.8 99.2-25.6 0 0 6.4-12.8 9.6-19.2 54.4-105.6 275.2-524.8 288-553.6 6.4-19.2-3.2-32-19.2-32C777.6 76.8 771.2 80 764.8 86.4z"></path></g></svg><div class="outlook-dropdown"><!----></div></button></div><div id="docsearch-container"></div><button class="toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span class="button-container"><span class="button-top"></span><span class="button-middle"></span><span class="button-bottom"></span></span></button><!--[--><!----><!--]--></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow left"></span></div><aside class="sidebar"><!--[--><!----><!--]--><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><p class="sidebar-heading active"><i class="icon iconfont icon-flower"></i><span class="title">文章</span><!----></p><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable active"><i class="icon iconfont icon-Tampermonkey"></i><span class="title">Tampermonkey</span><span class="arrow down"></span></button><ul class="sidebar-links"><li><!--[--><a aria-current="page" href="/article/tampermonkey/tm1.html" class="router-link-active router-link-exact-active nav-link active sidebar-link sidebar-page active" aria-label="油猴 API"><i class="icon iconfont icon-a-apichengxushezhichiluncanshu"></i>油猴 API<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#用户脚本标头-部分" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="用户脚本标头(部分)"><!---->用户脚本标头(部分)<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#api-说明" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="API 说明"><!---->API 说明<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#使用方法-示例" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="使用方法/示例"><!---->使用方法/示例<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#使用油猴gm-函数必须声明在沙盒环境中运行-grant-none" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="使用油猴GM_*函数必须声明在沙盒环境中运行 @grant none"><!---->使用油猴GM_*函数必须声明在沙盒环境中运行 @grant none<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-addstyle-gm-getresourcetext" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_addStyle &amp; GM_getResourceText"><!---->GM_addStyle &amp; GM_getResourceText<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-addelement" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_addElement"><!---->GM_addElement<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-set-get-deletevalue-gm-listvalues" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_set/get/deleteValue &amp; GM_listValues"><!---->GM_set/get/deleteValue &amp; GM_listValues<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-add-removevaluechangelistener" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_add/removeValueChangeListener"><!---->GM_add/removeValueChangeListener<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-reg-unregistermenucommand" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_reg/unregisterMenuCommand"><!---->GM_reg/unregisterMenuCommand<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-xmlhttprequest" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_xmlhttpRequest"><!---->GM_xmlhttpRequest<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-download" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_download"><!---->GM_download<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-notification-gm-openintab-gm-setclipboard" class="router-link-active router-link-exact-active nav-link sidebar-link heading" aria-label="GM_notification &amp; GM_openInTab &amp; GM_setClipboard"><!---->GM_notification &amp; GM_openInTab &amp; GM_setClipboard<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li></ul><!--]--></li><li><!--[--><a href="/article/tampermonkey/tm2.html" class="nav-link sidebar-link sidebar-page" aria-label="免费翻译API"><i class="icon iconfont icon-fanyi"></i>免费翻译API<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li><li><!--[--><a href="/article/tampermonkey/tm3.html" class="nav-link sidebar-link sidebar-page" aria-label="百度翻译API"><i class="icon iconfont icon-Group"></i>百度翻译API<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li></ul></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-javascript"></i><span class="title">JavaScript</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-css"></i><span class="title">CSS</span><span class="arrow right"></span></button><!----></section><!--]--></li></ul></section><!--]--></li></ul><!--[--><!----><!--]--></aside><!--[--><main class="page" id="main-content"><!----><nav class="breadcrumb disable"></nav><div class="page-title"><h1><i class="icon iconfont icon-a-apichengxushezhichiluncanshu"></i>油猴 API</h1><div class="page-info"><span class="author-info" aria-label="作者🖊" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon author-icon" viewbox="0 0 1024 1024" aria-labelledby="author"><title id="author" lang="en">author icon</title><g fill="currentColor"><path d="M649.6 633.6c86.4-48 147.2-144 147.2-249.6 0-160-128-288-288-288s-288 128-288 288c0 108.8 57.6 201.6 147.2 249.6-121.6 48-214.4 153.6-240 288-3.2 9.6 0 19.2 6.4 25.6 3.2 9.6 12.8 12.8 22.4 12.8h704c9.6 0 19.2-3.2 25.6-12.8 6.4-6.4 9.6-16 6.4-25.6-25.6-134.4-121.6-240-243.2-288z"></path></g></svg><span><span class="author-item">Hangover</span></span><span property="author" content="Hangover"></span></span><!----><span class="date-info" aria-label="写作日期📅" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewbox="0 0 1024 1024" aria-labelledby="calendar"><title id="calendar" lang="en">calendar icon</title><g fill="currentColor"><path d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z"></path></g></svg><span>2022年4月12日</span><meta property="datePublished" content="2022-04-12T00:00:00.000Z"></span><span class="category-info" aria-label="分类🌈" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon category-icon" viewbox="0 0 1024 1024" aria-labelledby="category"><title id="category" lang="en">category icon</title><g fill="currentColor"><path d="M148.41 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H148.41c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.311-40.31zM147.556 553.478H429.73c22.263 0 40.311 18.048 40.311 40.31v282.176c0 22.263-18.048 40.312-40.31 40.312H147.555c-22.263 0-40.311-18.049-40.311-40.312V593.79c0-22.263 18.048-40.311 40.31-40.311zM593.927 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H593.927c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.31-40.31zM730.22 920.502H623.926c-40.925 0-74.22-33.388-74.22-74.425V623.992c0-41.038 33.387-74.424 74.425-74.424h222.085c41.038 0 74.424 33.226 74.424 74.067v114.233c0 10.244-8.304 18.548-18.547 18.548s-18.548-8.304-18.548-18.548V623.635c0-20.388-16.746-36.974-37.33-36.974H624.13c-20.585 0-37.331 16.747-37.331 37.33v222.086c0 20.585 16.654 37.331 37.126 37.331H730.22c10.243 0 18.547 8.304 18.547 18.547 0 10.244-8.304 18.547-18.547 18.547z"></path></g></svg><ul class="categories-wrapper"><li class="category category5 clickable" role="navigation">Tampermonkey</li><meta property="articleSection" content="Tampermonkey"></ul></span><span aria-label="标签🏷" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon tag-icon" viewbox="0 0 1024 1024" aria-labelledby="tag"><title id="tag" lang="en">tag icon</title><g fill="currentColor"><path d="M939.902 458.563L910.17 144.567c-1.507-16.272-14.465-29.13-30.737-30.737L565.438 84.098h-.402c-3.215 0-5.726 1.005-7.634 2.913l-470.39 470.39a10.004 10.004 0 000 14.164l365.423 365.424c1.909 1.908 4.42 2.913 7.132 2.913s5.223-1.005 7.132-2.913l470.39-470.39c2.01-2.11 3.014-5.023 2.813-8.036zm-240.067-72.121c-35.458 0-64.286-28.828-64.286-64.286s28.828-64.285 64.286-64.285 64.286 28.828 64.286 64.285-28.829 64.286-64.286 64.286z"></path></g></svg><ul class="tags-wrapper"><li class="tag tag5 clickable" role="navigation">Tampermonkey</li><li class="tag tag2 clickable" role="navigation">油猴</li><li class="tag tag4 clickable" role="navigation">脚本猫</li></ul><meta property="keywords" content="Tampermonkey,油猴,脚本猫"></span><span class="reading-time-info" aria-label="阅读时间⌛" data-balloon-pos="down" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon timer-icon" viewbox="0 0 1024 1024" aria-labelledby="timer"><title id="timer" lang="en">timer icon</title><g fill="currentColor"><path d="M799.387 122.15c4.402-2.978 7.38-7.897 7.38-13.463v-1.165c0-8.933-7.38-16.312-16.312-16.312H256.33c-8.933 0-16.311 7.38-16.311 16.312v1.165c0 5.825 2.977 10.874 7.637 13.592 4.143 194.44 97.22 354.963 220.201 392.763-122.204 37.542-214.893 196.511-220.2 389.397-4.661 5.049-7.638 11.651-7.638 19.03v5.825h566.49v-5.825c0-7.379-2.849-13.981-7.509-18.9-5.049-193.016-97.867-351.985-220.2-389.527 123.24-37.67 216.446-198.453 220.588-392.892zM531.16 450.445v352.632c117.674 1.553 211.787 40.778 211.787 88.676H304.097c0-48.286 95.149-87.382 213.728-88.676V450.445c-93.077-3.107-167.901-81.297-167.901-177.093 0-8.803 6.99-15.793 15.793-15.793 8.803 0 15.794 6.99 15.794 15.793 0 80.261 63.69 145.635 142.01 145.635s142.011-65.374 142.011-145.635c0-8.803 6.99-15.793 15.794-15.793s15.793 6.99 15.793 15.793c0 95.019-73.789 172.82-165.96 177.093z"></path></g></svg><span>大约 4 分钟</span><meta property="timeRequired" content="PT4M"></span></div><hr></div><div class="toc-place-holder"><aside id="toc-list"><div class="toc-header">此页内容</div><div class="toc-wrapper"><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#用户脚本标头-部分" class="router-link-active router-link-exact-active toc-link level2">用户脚本标头(部分)</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#api-说明" class="router-link-active router-link-exact-active toc-link level2">API 说明</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#使用方法-示例" class="router-link-active router-link-exact-active toc-link level2">使用方法/示例</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#使用油猴gm-函数必须声明在沙盒环境中运行-grant-none" class="router-link-active router-link-exact-active toc-link level3">使用油猴GM_*函数必须声明在沙盒环境中运行 @grant none</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-addstyle-gm-getresourcetext" class="router-link-active router-link-exact-active toc-link level3">GM_addStyle &amp; GM_getResourceText</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-addelement" class="router-link-active router-link-exact-active toc-link level3">GM_addElement</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-set-get-deletevalue-gm-listvalues" class="router-link-active router-link-exact-active toc-link level3">GM_set/get/deleteValue &amp; GM_listValues</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-add-removevaluechangelistener" class="router-link-active router-link-exact-active toc-link level3">GM_add/removeValueChangeListener</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-reg-unregistermenucommand" class="router-link-active router-link-exact-active toc-link level3">GM_reg/unregisterMenuCommand</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-xmlhttprequest" class="router-link-active router-link-exact-active toc-link level3">GM_xmlhttpRequest</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-download" class="router-link-active router-link-exact-active toc-link level3">GM_download</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/article/tampermonkey/tm1.html#gm-notification-gm-openintab-gm-setclipboard" class="router-link-active router-link-exact-active toc-link level3">GM_notification &amp; GM_openInTab &amp; GM_setClipboard</a></li><!----><!--]--></ul><!--]--></ul></div></aside></div><!----><div class="theme-hope-content"><!--[--><div class="custom-container info"><p class="custom-container-title">介绍如何使用 Tampermonkey API</p></div><h2 id="用户脚本标头-部分" tabindex="-1"><a class="header-anchor" href="#用户脚本标头-部分" aria-hidden="true">#</a> 用户脚本标头(部分)</h2><table><thead><tr><th style="text-align:left;">名称</th><th style="text-align:left;">描述</th><th style="text-align:left;">参数</th></tr></thead><tbody><tr><td style="text-align:left;"><code>name</code></td><td style="text-align:left;">脚本名称</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>namespace</code></td><td style="text-align:left;">脚本命名空间</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>version</code></td><td style="text-align:left;">脚本版本</td><td style="text-align:left;"><a href="https://semver.org/lang/zh-CN/" target="_blank" rel="noopener noreferrer">语义化版本规则<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a></td></tr><tr><td style="text-align:left;"><code>author</code></td><td style="text-align:left;">脚本作者</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>description</code></td><td style="text-align:left;">脚本描述</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>include</code></td><td style="text-align:left;">脚本匹配地址</td><td style="text-align:left;">允许正则匹配</td></tr><tr><td style="text-align:left;"><code>match</code></td><td style="text-align:left;">脚本匹配地址</td><td style="text-align:left;">使用*表示通配，使用更严格</td></tr><tr><td style="text-align:left;"><code>exclude</code></td><td style="text-align:left;">排除脚本匹配地址</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>require</code></td><td style="text-align:left;">引入外部JS文件</td><td style="text-align:left;">指向脚本开始运行之前加载并执行的 JavaScript 文件</td></tr><tr><td style="text-align:left;"><code>resource</code></td><td style="text-align:left;">预加载资源</td><td style="text-align:left;">预加载的资源由GM_getResourceURL/Text访问</td></tr><tr><td style="text-align:left;"><code>connect</code></td><td style="text-align:left;">获取网站访问权限</td><td style="text-align:left;">允许由GM_xmlhttpRequest检索的子域</td></tr><tr><td style="text-align:left;"><code>run-at</code></td><td style="text-align:left;">脚本的运行时机</td><td style="text-align:left;">document-start/body/end/idle/menu <a href="https://docs.scriptcat.org/dev/meta.html#run-at" target="_blank" rel="noopener noreferrer">详细<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a></td></tr><tr><td style="text-align:left;"><code>grant</code></td><td style="text-align:left;">申请API权限</td><td style="text-align:left;">none表示页面环境 unsafeWindow表示沙盒环境</td></tr><tr><td style="text-align:left;"><code>noframes</code></td><td style="text-align:left;">脚本标记</td><td style="text-align:left;">标记使脚本在主页上运行，但不在 iframe 上运行</td></tr></tbody></table><h2 id="api-说明" tabindex="-1"><a class="header-anchor" href="#api-说明" aria-hidden="true">#</a> API 说明</h2><table><thead><tr><th style="text-align:left;">名称</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;"><code>GM_addStyle</code></td><td style="text-align:left;">将给定样式添加到文档中并返回注入的样式元素</td></tr><tr><td style="text-align:left;"><code>GM_addElement</code></td><td style="text-align:left;">创建指定的 HTML 元素，应用所有给定的&quot;属性&quot;并返回注入的 HTML 元素，此功能是实验性的，API 可能会更改</td></tr><tr><td style="text-align:left;"><code>GM_setValue</code></td><td style="text-align:left;">将&quot;名称&quot;的值设置为存储</td></tr><tr><td style="text-align:left;"><code>GM_getValue</code></td><td style="text-align:left;">从 GM_setValue 存储的&quot;名称&quot;中获取值</td></tr><tr><td style="text-align:left;"><code>GM_deleteValue</code></td><td style="text-align:left;">将 GM_setValue 存储的&quot;名称&quot;删除</td></tr><tr><td style="text-align:left;"><code>GM_listValues</code></td><td style="text-align:left;">列出GM_setValue 存储的所有&quot;名称&quot;</td></tr><tr><td style="text-align:left;"><code>GM_addValueChangeListener</code></td><td style="text-align:left;">侦听 GM_setValue 储存&quot;名称&quot;的值的更改并返回更改前和后的值</td></tr><tr><td style="text-align:left;"><code>GM_removeValueChangeListener</code></td><td style="text-align:left;">删除由 GM_addValueChangeListener 添加的侦听器</td></tr><tr><td style="text-align:left;"><code>GM_log</code></td><td style="text-align:left;">向控制台记录消息</td></tr><tr><td style="text-align:left;"><code>GM_getResourceText</code></td><td style="text-align:left;">获取由 resource 预加载的资源</td></tr><tr><td style="text-align:left;"><code>GM_getResourceURL</code></td><td style="text-align:left;">获取由 resource 预加载的 base64 编码 URI</td></tr><tr><td style="text-align:left;"><code>GM_registerMenuCommand</code></td><td style="text-align:left;">注册一个菜单，在运行此脚本的页面中显示</td></tr><tr><td style="text-align:left;"><code>GM_registerMenuCommand</code></td><td style="text-align:left;">取消由 GM_registerMenuCommand 注册的菜单</td></tr><tr><td style="text-align:left;"><code>GM_openInTab</code></td><td style="text-align:left;">通过给定的 URL 打开一个新标签页</td></tr><tr><td style="text-align:left;"><code>GM_xmlhttpRequest</code></td><td style="text-align:left;">通过脚本发送的XHR请求</td></tr><tr><td style="text-align:left;"><code>GM_download</code></td><td style="text-align:left;">通过给定的 URL 下载文件到本地</td></tr><tr><td style="text-align:left;"><code>GM_saveTab</code></td><td style="text-align:left;">保存选项卡对象，生命周期为选项卡的打开-&gt;关闭</td></tr><tr><td style="text-align:left;"><code>GM_getTab</code></td><td style="text-align:left;">获取选项卡对象，生命周期为选项卡的打开-&gt;关闭</td></tr><tr><td style="text-align:left;"><code>GM_getTabs</code></td><td style="text-align:left;">获取所有选项卡对象，生命周期为选项卡的打开-&gt;关闭</td></tr><tr><td style="text-align:left;"><code>GM_notification</code></td><td style="text-align:left;">显示 HTML5 桌面通知</td></tr><tr><td style="text-align:left;"><code>GM_setClipboard</code></td><td style="text-align:left;">将数据复制到剪贴板</td></tr></tbody></table><h2 id="使用方法-示例" tabindex="-1"><a class="header-anchor" href="#使用方法-示例" aria-hidden="true">#</a> 使用方法/示例</h2><h3 id="使用油猴gm-函数必须声明在沙盒环境中运行-grant-none" tabindex="-1"><a class="header-anchor" href="#使用油猴gm-函数必须声明在沙盒环境中运行-grant-none" aria-hidden="true">#</a> 使用油猴GM_*函数必须声明在沙盒环境中运行 <s>@grant none</s></h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        unsafeWindow</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="gm-addstyle-gm-getresourcetext" tabindex="-1"><a class="header-anchor" href="#gm-addstyle-gm-getresourcetext" aria-hidden="true">#</a> GM_addStyle &amp; GM_getResourceText</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_addStyle</span>
<span class="token comment">// @grant        GM_getResourceText</span>
<span class="token comment">// @resource css https://cdn.jsdelivr.net/npm/index.css</span>

<span class="token function">GM_addStyle</span><span class="token punctuation">(</span><span class="token function">GM_getResourceText</span><span class="token punctuation">(</span>css<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token function">GM_addStyle</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">
    body{
        background-color: orange;
    }
</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="gm-addelement" tabindex="-1"><a class="header-anchor" href="#gm-addelement" aria-hidden="true">#</a> GM_addElement</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_addElement</span>

<span class="token function">GM_addElement</span><span class="token punctuation">(</span><span class="token string">&#39;script&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">src</span><span class="token operator">:</span> <span class="token string">&#39;https://cdn.jsdelivr.net/npm/index.js&#39;</span><span class="token punctuation">,</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">&#39;text/javascript&#39;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">GM_addElement</span><span class="token punctuation">(</span><span class="token string">&#39;link&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">href</span><span class="token operator">:</span> <span class="token string">&#39;https://cdn.jsdelivr.net/npm/index.css&#39;</span><span class="token punctuation">,</span>
    <span class="token literal-property property">rel</span><span class="token operator">:</span> <span class="token string">&#39;stylesheet&#39;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="gm-set-get-deletevalue-gm-listvalues" tabindex="-1"><a class="header-anchor" href="#gm-set-get-deletevalue-gm-listvalues" aria-hidden="true">#</a> GM_set/get/deleteValue &amp; GM_listValues</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_setValue</span>
<span class="token comment">// @grant        GM_getValue</span>
<span class="token comment">// @grant        GM_listValues</span>
<span class="token comment">// @grant        GM_deleteValue</span>

<span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token string">&quot;hello world&quot;</span>

<span class="token function">GM_setValue</span><span class="token punctuation">(</span><span class="token string">&quot;value&quot;</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span>        <span class="token comment">// 将&quot;名称&quot;的值设置为存储</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">GM_getValue</span><span class="token punctuation">(</span><span class="token string">&quot;value&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment">// 从存储中获取&quot;名称&quot;的值 =&gt; hello world</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">GM_listValues</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>       <span class="token comment">// 列出存储的所有名称 =&gt; [&#39;value&#39;]</span>

<span class="token function">GM_deleteValue</span><span class="token punctuation">(</span><span class="token string">&quot;value&quot;</span><span class="token punctuation">)</span>            <span class="token comment">// 从存储中删除&quot;名称&quot;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">GM_listValues</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>       <span class="token comment">// =&gt; []</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="gm-add-removevaluechangelistener" tabindex="-1"><a class="header-anchor" href="#gm-add-removevaluechangelistener" aria-hidden="true">#</a> GM_add/removeValueChangeListener</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_setValue</span>
<span class="token comment">// @grant        GM_addValueChangeListener</span>
<span class="token comment">// @grant        GM_removeValueChangeListener</span>

<span class="token function">GM_addValueChangeListener</span><span class="token punctuation">(</span><span class="token string">&#39;value&#39;</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> old_value<span class="token punctuation">,</span> new_value<span class="token punctuation">,</span> remote</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> old_value<span class="token punctuation">,</span> new_value<span class="token punctuation">,</span> remote<span class="token punctuation">)</span>

    <span class="token comment">// 3秒后输出 =&gt;value old_value new_value false</span>

<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

    <span class="token function">GM_setValue</span><span class="token punctuation">(</span><span class="token string">&#39;value&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;new_value&#39;</span><span class="token punctuation">)</span>

<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">3000</span><span class="token punctuation">)</span>

<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>

    <span class="token function">GM_removeValueChangeListener</span><span class="token punctuation">(</span>add<span class="token punctuation">)</span>  <span class="token comment">// 按 ID 删除侦听器</span>

    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;已删除&#39;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">GM_setValue</span><span class="token punctuation">(</span><span class="token string">&#39;value&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;new_value1&#39;</span><span class="token punctuation">)</span> <span class="token comment">// 这里改变&#39;value&#39;的值后侦听器不会再执行</span>

<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">6000</span><span class="token punctuation">)</span>

</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h3 id="gm-reg-unregistermenucommand" tabindex="-1"><a class="header-anchor" href="#gm-reg-unregistermenucommand" aria-hidden="true">#</a> GM_reg/unregisterMenuCommand</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_registerMenuCommand</span>
<span class="token comment">// @grant        GM_unregisterMenuCommand</span>

<span class="token keyword">let</span> menu <span class="token operator">=</span> <span class="token function">GM_registerMenuCommand</span><span class="token punctuation">(</span><span class="token string">&#39;hello world&#39;</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&#39;hello world&#39;</span><span class="token punctuation">)</span>

    <span class="token function">GM_unregisterMenuCommand</span><span class="token punctuation">(</span>menu<span class="token punctuation">)</span> <span class="token comment">// 按id删除一个菜单</span>

<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&#39;H&#39;</span><span class="token punctuation">)</span> <span class="token comment">// 快捷键</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="gm-xmlhttprequest" tabindex="-1"><a class="header-anchor" href="#gm-xmlhttprequest" aria-hidden="true">#</a> GM_xmlhttpRequest</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_xmlhttpRequest</span>

<span class="token keyword">let</span> ajax <span class="token operator">=</span> <span class="token function">GM_xmlhttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">{</span>

    <span class="token literal-property property">method</span><span class="token operator">:</span> <span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span>                        <span class="token comment">// 请求方法 GET POST</span>

    <span class="token comment">// headers: {},                       // 消息头</span>

    <span class="token comment">// data: &#39;&#39;,                          // 通过 POST 请求发送的字符串</span>

    <span class="token comment">// timeout: 10000,                    // 超时（毫秒）</span>

    <span class="token literal-property property">responseType</span><span class="token operator">:</span> <span class="token string">&quot;json&quot;</span><span class="token punctuation">,</span>                 <span class="token comment">// 响应的数据类型 text arraybuffer blob document json</span>

    <span class="token comment">// overrideMimeType: &quot;text/xml&quot;,      // 请求的 MIME 类型</span>

    <span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">&#39;url&#39;</span><span class="token punctuation">,</span>                           <span class="token comment">// 请求的 URL</span>

    <span class="token function-variable function">onabort</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>              <span class="token comment">// 如果请求中止，则要执行的回调</span>

    <span class="token function-variable function">onerror</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>              <span class="token comment">// 如果请求最终出现错误，则要执行的回调</span>

    <span class="token function-variable function">onloadstart</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>          <span class="token comment">// 在请求开始时执行的回调</span>

    <span class="token function-variable function">onprogress</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>           <span class="token comment">// 如果请求取得了一些进展，则要执行的回调</span>

    <span class="token function-variable function">onreadystatechange</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>   <span class="token comment">// 在请求的就绪状态发生更改时要执行的回调</span>

    <span class="token function-variable function">ontimeout</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>            <span class="token comment">// 如果请求由于超时而失败，则要执行的回调</span>

    <span class="token function-variable function">onload</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">xhr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>              <span class="token comment">// 如果加载了请求，则要执行的回调</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>xhr<span class="token punctuation">)</span><span class="token punctuation">;</span>
      
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">abort</span><span class="token punctuation">(</span>ajax<span class="token punctuation">)</span>                               <span class="token comment">// 调用以取消此请求</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h3 id="gm-download" tabindex="-1"><a class="header-anchor" href="#gm-download" aria-hidden="true">#</a> GM_download</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_download</span>

<span class="token keyword">let</span> download <span class="token operator">=</span> <span class="token function">GM_download</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  
    <span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">&#39;url&#39;</span><span class="token punctuation">,</span>                           <span class="token comment">// 下载文件的 URL 地址</span>

    <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;文件名.后缀&quot;</span><span class="token punctuation">,</span>                   <span class="token comment">// 不填则自动获取文件名</span>

    <span class="token literal-property property">saveAs</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>                         <span class="token comment">// 布尔值，显示&quot;保存为&quot;对话框</span>

    <span class="token function-variable function">onerror</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>           <span class="token comment">// 如果下载最终出现错误，则要执行的回调</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>

    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function-variable function">onprogress</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">pro</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>                <span class="token comment">// 如果此下载取得了一些进展，则要执行的回调</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pro<span class="token punctuation">.</span>loaded<span class="token punctuation">)</span>           <span class="token comment">// 文件加载量</span>

        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pro<span class="token punctuation">.</span>totalSize<span class="token punctuation">)</span>        <span class="token comment">// 文件总大小</span>

    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function-variable function">ontimeout</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>                  <span class="token comment">// 如果此下载由于超时而失败，则要执行的回调</span>
    
    <span class="token function-variable function">onload</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>                      <span class="token comment">// 如果此下载完成，则要执行的回调</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">abort</span><span class="token punctuation">(</span>download<span class="token punctuation">)</span>                           <span class="token comment">// 调用以取消此下载</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h3 id="gm-notification-gm-openintab-gm-setclipboard" tabindex="-1"><a class="header-anchor" href="#gm-notification-gm-openintab-gm-setclipboard" aria-hidden="true">#</a> GM_notification &amp; GM_openInTab &amp; GM_setClipboard</h3><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// @grant        GM_openInTab</span>
<span class="token comment">// @grant        GM_notification</span>
<span class="token comment">// @grant        GM_setClipboard</span>

<span class="token function">GM_notification</span><span class="token punctuation">(</span><span class="token punctuation">{</span>

    <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">&quot;标题&quot;</span><span class="token punctuation">,</span>

    <span class="token literal-property property">image</span><span class="token operator">:</span> <span class="token string">&quot;图像链接&quot;</span><span class="token punctuation">,</span>

    <span class="token literal-property property">text</span><span class="token operator">:</span> <span class="token string">&quot;通知内容&quot;</span><span class="token punctuation">,</span>
    
    <span class="token literal-property property">highlight</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>                   <span class="token comment">// 布尔值，是否突出显示发送通知的选项卡</span>

    <span class="token literal-property property">silent</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>                     <span class="token comment">// 布尔值，是否播放声音</span>

    <span class="token literal-property property">timeout</span><span class="token operator">:</span> <span class="token number">10000</span><span class="token punctuation">,</span>                    <span class="token comment">// 设置通知隐藏时间</span>

    <span class="token function-variable function">onclick</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>             <span class="token comment">// 在单击通知时调用</span>

        <span class="token function">GM_openInTab</span><span class="token punctuation">(</span><span class="token string">&quot;url&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>      <span class="token comment">// 使用此 URL打开一个新标签页</span>
        
        <span class="token comment">//or</span>

        <span class="token function">GM_setClipboard</span><span class="token punctuation">(</span><span class="token string">&quot;text&quot;</span><span class="token punctuation">)</span>        <span class="token comment">// 将数据复制到剪贴板</span>

    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function">ondone</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>                        <span class="token comment">// 在通知关闭（无论这是由超时还是单击触发）或突出显示选项卡时调用</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><!--]--></div><!----><footer class="page-meta"><!----><!----><!----></footer><nav class="page-nav"><!----><a href="/article/tampermonkey/tm2.html" class="nav-link next" aria-label="免费翻译API"><div class="hint">下一页<span class="arrow right"></span></div><div class="link">免费翻译API<i class="icon iconfont icon-fanyi"></i></div></a></nav><div class="giscus-wrapper input-top" style="display:block;"><giscus-widget repo="zuifanzhangsan/giscus-blog" repoId="R_kgDOHOyKyA" category="Announcements" categoryId="DIC_kwDOHOyKyM4COw_c" lang="zh-CN" theme="light" mapping="pathname" inputPosition="top" reactionsEnabled="1" emitMetadata="0"></giscus-widget></div><!----></main><!--]--><footer class="footer-wrapper"><div class="footer">鸟无声兮山寂寂，夜正长兮风淅淅。 ──李华《吊古战场文》</div><div class="copyright">Copyright © 2023 Hangover</div></footer></div><!--]--><!----><!--]--></div>
    <script type="module" src="/assets/app.0d0ab3c5.js" defer></script>
  </body>
</html>
